package com.project.isaacwiki.controller;

import com.project.isaacwiki.dto.UserRegister;
import com.project.isaacwiki.service.ServiceLogin;
import com.project.isaacwiki.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class ControllerLogin {

    @Autowired
    private ServiceLogin serviceLogin;

    @PostMapping("/login")
    public Result login(String username, String password)
    {
        return serviceLogin.login(username, password);
    }

    @PostMapping("/uploadImage")
    public Result uploadImage(MultipartFile file) throws Exception
    {
        return serviceLogin.uploadImage(file);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserRegister userRegister) throws Exception
    {
        try {
            return serviceLogin.register(userRegister);
        } catch (Exception e) {
            return Result.Error(e.getMessage());
        }
    }
}
